/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bis;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import obj.CommonVar;

/**
 *
 * @author Administrator
 */
public class FLBaseMgr {

    protected String UpdateStr,  InsertStr,  DeleteStr,  QueryStr;
    protected String bm;
    private ResultSet rs = null;
    private int id;

    public void setBm(String bm) {
        this.bm = bm;
    }

    public void BaseUpdate() {
        rs = Query();
        try {
            if (rs.next()) {
                Update();
            }
        } catch (SQLException ex) {
            Logger.getLogger(FLBookClass.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                rs.close();
            } catch (SQLException ex) {
                Logger.getLogger(FLBookClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void BaseDelete() {
        ResultSet rs = Query();
        try {
            if (rs.next()) {
                Update();
            }
        } catch (SQLException ex) {
            Logger.getLogger(FLBookClass.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                rs.close();
            } catch (SQLException ex) {
                Logger.getLogger(FLBookClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public int Update() {
        int executeUpdate = 0;
        try {
            executeUpdate = CommonVar.commondb.stmt.executeUpdate(UpdateStr);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return executeUpdate;
    }

    public boolean Insert() {
        int executeUpdate = 23445;
        try {
            executeUpdate = CommonVar.commondb.stmt.executeUpdate(InsertStr);
        } catch (SQLException ex) {
            Logger.getLogger(FLBaseMgr.class.getName()).log(Level.SEVERE, null, ex);
        }
        return executeUpdate == 1;
    }

    public int getmaxbyid(String s) {
        try {
            rs = CommonVar.commondb.stmt.executeQuery("select max(id) from " + s);
            if (rs.next()) {
                id = rs.getInt(1);
            }

        } catch (SQLException ex) {
            Logger.getLogger(FLBaseMgr.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            CommonVar.commondb.dcm.closeResultSet(rs);
            return id;
        }
    }

    public int getmaxbybm(String s) {
      try {
            s="select bm from " + s;
            rs = CommonVar.commondb.stmt.executeQuery(s);
            if (rs.last()) {
                id = rs.getInt(1);
            }

        } catch (SQLException ex) {
            Logger.getLogger(FLBaseMgr.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            CommonVar.commondb.dcm.closeResultSet(rs);
            //CommonVar.commondb.dcm.closeStatement(CommonVar.commondb.stmt);
            return id+1;
        }
    }

    public boolean Delete() {
        boolean execute = false;
        try {
            execute = CommonVar.commondb.stmt.execute(DeleteStr);
        } catch (SQLException ex) {
            Logger.getLogger(FLBaseMgr.class.getName()).log(Level.SEVERE, null, ex);
        }
        return execute;
    }

    public ResultSet Query() {
        ResultSet executeQuery = null;
        try {
            executeQuery = CommonVar.commondb.stmt.executeQuery(QueryStr);
        } catch (SQLException ex) {
            Logger.getLogger(FLBaseMgr.class.getName()).log(Level.SEVERE, null, ex);
        }
        return executeQuery;
    }
}
